﻿using FactoryDeviceSystem.Models;
using FactoryDeviceSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FactoryDeviceSystem
{
    public partial class StoreSettingForm : Form
    {
        private List<StoreSet> storeSets;
        public StoreSettingForm()
        {
            InitializeComponent();
        }

        bool isSaved = true;
        StoreSet storeSet;

        private void StoreSettingForm_Load(object sender, EventArgs e)
        {
            InitStoreSet();
        }

        private void InitStoreSet()
        {
            storeSets = ConfigUtil.LoadStores();
            if (storeSets.Count > 0)
            {   
                infoTypeCbBox.DataSource = storeSets;
                infoTypeCbBox.DisplayMember = "ParaText";
                infoTypeCbBox.ValueMember = "ParaType";
                infoTypeCbBox.SelectedIndex = 0;
   
                posTxt.Text = storeSets[0].FirstPosition.ToString();

                dataTypeCbBox.DataSource = storeSets.Select(s=>s.ReadType).Distinct().ToList();
                dataTypeCbBox.Text = storeSets[0].ReadType;


                storeSet = storeSets[0];
                LoadListView();
            }
        }

        private void LoadListView()
        {
            lsView.Items.Clear();
            foreach (StoreSet item in storeSets)
            {
                ListViewItem listViewItem = new ListViewItem(item.ParaText.ToString());
                listViewItem.SubItems.Add(item.ParaType.ToString());
                listViewItem.SubItems.Add(item.FirstPosition.ToString());
                listViewItem.SubItems.Add(item.ReadType.ToString());
                lsView.Items.Add(listViewItem);
            }
        }

        private void Confirm_Click(object sender, EventArgs e)
        {
          
            storeSet.ParaText = infoTypeCbBox.Text.ToString();
            storeSet.ParaType = infoTypeCbBox.SelectedValue.ToString();
            storeSet.FirstPosition = int.Parse(posTxt.Text.ToString());
            storeSet.ReadType = dataTypeCbBox.Text.ToString();

            if(!storeSets.Any(s => s.ParaText == storeSet.ParaText))
            {
                MessageBox.Show($"当前配置中不存在名称为：{storeSet.ParaText}的配置");
                return;
            }

            MessageBox.Show("数据修改成功");
            LoadListView();
            SaveDataToFile();
            isSaved = true;
        }

        private void SaveDataToFile()
        {
            string data = JsonConvert.SerializeObject(storeSets, Formatting.Indented);
            string filePath = Application.StartupPath + "\\ConfigFiles\\StoreSet.json";
            File.WriteAllText(filePath,data,Encoding.UTF8);
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            if (!isSaved)
            {
                DialogResult result = MessageBox.Show("当前内容并未保存确认关闭吗？","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if(result == DialogResult.Yes)
                {
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }

        private void infoTypeCbBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            isSaved = false;
        }

        private void posTxt_TextChanged(object sender, EventArgs e)
        {
            isSaved = false;
        }

        private void dataTypeCbBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            isSaved = false;
        }

        private void lsView_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = lsView.SelectedItems;
            if(items.Count == 0)
                return;
            int idx = items[0].Index;
            storeSet = storeSets[idx];
            infoTypeCbBox.Text = storeSet.ParaText;
            infoTypeCbBox.SelectedValue = storeSet.ParaType;
            posTxt.Text = storeSet.FirstPosition.ToString();
            dataTypeCbBox.Text = storeSet.ReadType;
        }
    }
}
